﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using CoiffeurProject.Screens;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CoiffeurProject.UserControls
{
    internal class CustomersFiltersPanel
    {
        Grid main_container;
        ComboBox sex, age, frequency, bills_status;
        CustomerScreen screen;
        Button reset;

        internal CustomersFiltersPanel(CustomerScreen screen)
        {
            this.screen = screen;

            InitializeGrid();

            LoadComboBoxes();

            LoadButton();
        }

        private void LoadButton()
        {
            reset = new Button()
            {
                Width = 57,
                Height = 14,
                Margin = new Thickness(300, 13, 0, 0),
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Content = new Image() { Source = new BitmapImage(new Uri(
                    @"pack://application:,,,/CoiffeurProject;component/Images/Customer/Icons/reset.PNG")), 
                    Stretch = Stretch.Fill }
            };

            reset.SetResourceReference(Button.StyleProperty, "NoChromeButton");

            reset.Click += new RoutedEventHandler(reset_Click);

            main_container.Children.Add(reset);
        }

        void reset_Click(object sender, RoutedEventArgs e)
        {
            sex.SelectedIndex = 0;
            age.SelectedIndex = 0;
            frequency.SelectedIndex = 0;
            bills_status.SelectedIndex = 0;

            ApplyOtherFilter(screen.GetFilteredCustomersList(), -1);
        }

        private void InitializeGrid()
        {
            main_container = new Grid()
            {
                Width = 368,
                Height = 255,
                Margin = new Thickness(280, 291, 0, 0),
                VerticalAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Left
            };

            Grid.SetColumn(main_container, 2);
            Grid.SetRow(main_container, 1);
        }

        private ComboBox LoadComboBox(ComboBoxItem[] items, int num, string name)
        {
            var box = new ComboBox()
            {
                Width = 345,
                Height = 40,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(20, 45 + 50 * num, 0, 0),
                SelectedIndex = 0,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Center,
                Foreground = new SolidColorBrush(Colors.White),
                FontFamily = new FontFamily("Segoe UI"),
                FontSize = 20,
                Name = name
            };

            box.ItemsSource = items;

            for (int i = 0; i < box.Items.Count; i++)
                (box.Items[i] as ComboBoxItem).SetResourceReference(ComboBoxItem.StyleProperty, 
                    "SimpleComboBoxItem");

            box.SelectionChanged += new SelectionChangedEventHandler(BoxSelectionChanged);

            main_container.Children.Add(box);

            return box;
        }

        void BoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cb = sender as ComboBox;
            var curr_list = screen.GetFilteredCustomersList();

            switch (cb.Name)
            { 
                case "sex":
                    switch (cb.SelectedIndex)
                    {
                        case 1:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Sex == "Maschio").ToList();
                            break;
                        case 2:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Sex == "Femmina").ToList();
                            break;
                    }
                    ApplyOtherFilter(curr_list, 0);
                    break;
                case "age":
                    switch (cb.SelectedIndex)
                    {
                        case 1:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age <= 20).ToList();
                            break;
                        case 2:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 20 &&
                                    c.LinkedCustomer.Age <= 30).ToList();
                            break;
                        case 3:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 30 &&
                                    c.LinkedCustomer.Age <= 40).ToList();
                            break;
                        case 4:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 40 &&
                                    c.LinkedCustomer.Age <= 50).ToList();
                            break;
                        case 5:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 50 &&
                                    c.LinkedCustomer.Age <= 60).ToList();
                            break;
                        case 6:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 60).ToList();
                            break;
                    }
                    ApplyOtherFilter(curr_list, 1);
                    break;
                case "freq":
                    switch (cb.SelectedIndex)
                    {
                        case 1:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Frequency >= 10).ToList();
                            break;
                        case 2:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Frequency <= 9).ToList();
                            break;
                        case 3:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Frequency <= 5).ToList();
                            break;
                        case 4:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Frequency <= 3).ToList();
                            break;
                    }
                    ApplyOtherFilter(curr_list, 2);
                    break;
                case "bill":
                    switch (cb.SelectedIndex)
                    {
                        case 1:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Debtor).ToList();
                            break;
                        case 2:
                            curr_list = curr_list.Where(c => !c.LinkedCustomer.Debtor).ToList();
                            break;
                    }
                    ApplyOtherFilter(curr_list, 3);
                    break;
            }
        }

        private void ApplyOtherFilter(List<SVCustomerEntry> curr_list, int p)
        {
            if (p != 0)
                    switch (sex.SelectedIndex)
                    {
                        case 1:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Sex == "Maschio").ToList();
                            break;
                        case 2:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Sex == "Femmina").ToList();
                            break;
                    }

                if (p != 1)
                    switch (age.SelectedIndex)
                    {
                        case 1:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age <= 20).ToList();
                            break;
                        case 2:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 20 &&
                                    c.LinkedCustomer.Age <= 30).ToList();
                            break;
                        case 3:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 30 &&
                                    c.LinkedCustomer.Age <= 40).ToList();
                            break;
                        case 4:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 40 &&
                                    c.LinkedCustomer.Age <= 50).ToList();
                            break;
                        case 5:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 50 &&
                                    c.LinkedCustomer.Age <= 60).ToList();
                            break;
                        case 6:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Age > 60).ToList();
                            break;
                    }

                if (p != 2)
                    switch (frequency.SelectedIndex)
                    {
                        case 1:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Frequency >= 10).ToList();
                            break;
                        case 2:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Frequency <= 9).ToList();
                            break;
                        case 3:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Frequency <= 5).ToList();
                            break;
                        case 4:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Frequency <= 3).ToList();
                            break;
                    }

                if (p != 3)
                    switch (bills_status.SelectedIndex)
                    {
                        case 1:
                            curr_list = curr_list.Where(c => c.LinkedCustomer.Debtor).ToList();
                            break;
                        case 2:
                            curr_list = curr_list.Where(c => !c.LinkedCustomer.Debtor).ToList();
                            break;
                    }

                screen.AddCustomersToSV(curr_list);
        }

        private void LoadComboBoxes()
        {
            sex = LoadComboBox(
                new ComboBoxItem[] { 
                    new ComboBoxItem() { Content = "Uomini e Donne"},
                    new ComboBoxItem() { Content = "Solo Uomini"},
                    new ComboBoxItem() { Content = "Solo Donne"},
                }, 0, "sex");

            age = LoadComboBox(
                new ComboBoxItem[] { 
                    new ComboBoxItem() { Content = "Tutte le età"},
                    new ComboBoxItem() { Content = "Fino a 20 anni"},
                    new ComboBoxItem() { Content = "Da 20 a 30 anni"},
                    new ComboBoxItem() { Content = "Da 30 a 40 anni"},
                    new ComboBoxItem() { Content = "Da 40 a 50 anni"},
                    new ComboBoxItem() { Content = "Da 50 a 60 anni"},
                    new ComboBoxItem() { Content = "Più di 60 anni"},
                }, 1, "age");

            frequency = LoadComboBox(
                new ComboBoxItem[] { 
                    new ComboBoxItem() { Content = "Qualsiasi frequenza"},
                    new ComboBoxItem() { Content = "10 o più volte in un anno"},
                    new ComboBoxItem() { Content = "Fino a 9 volte in un anno"},
                    new ComboBoxItem() { Content = "Fino a 5 volte in un anno"},
                    new ComboBoxItem() { Content = "Fino a 3 volte in un anno"},
                }, 2, "freq");

            bills_status = LoadComboBox(
                new ComboBoxItem[] { 
                    new ComboBoxItem() { Content = "Status del conto"},
                    new ComboBoxItem() { Content = "Debitori"},
                    new ComboBoxItem() { Content = "Saldati"},
                }, 3, "bill");
        }

        internal Grid MainContainer
        {
            get { return main_container; }
        }
    }
}
